package com.tools.web.interceptor.auth._common;

import com.tools.common.object.Note;
import com.tools.common.object.json.JsonKit;
import com.tools.common.thread.Timeunit;
import com.tools.middle.redis.client.JedisClusterPlus;
import com.tools.web.interceptor.auth.*;

import java.util.Objects;

/**
 * JedisClusterPlus 集群客户端操作 Redis 集群实现的 “用户单在线记录” 的 AuthenticationManager 身份认证拦截器
 * */
@Note("JedisClusterPlus 集群客户端操作 Redis 集群实现的 “用户单在线记录” 的 AuthenticationManager 身份认证拦截器")
public abstract class JedisClusterPlusSingleAuthenticationManager extends GenericAuthenticationManager implements SingleRedisLuaScript {

    @Note("操作 Redis 集群的 JedisClusterPlus 集群客户端，依靠 JedisCluster 实现")
    private final JedisClusterPlus jedisClusterPlus;

    protected JedisClusterPlusSingleAuthenticationManager(JedisClusterPlus jedisClusterPlus, AuthSource source, AuthUserService authUserService) {
        super(source, authUserService);
        this.jedisClusterPlus = Objects.requireNonNull(jedisClusterPlus, "操作 Redis 集群的 JedisClusterPlus 集群客户端为 null");
    }

    @Override
    public final Timeunit getTimeunitUsedBySubclass() {
        return Timeunit.SECOND;
    }

    @Override
    protected GetAuthUserResult getAuthUser(String key, AuthToken decryptedAuthToken) {
        String json = (String) this.jedisClusterPlus.get(key);
        if(json == null) return super.protected_getResultIfExpired(decryptedAuthToken);
        SingleAuthUser authUser = JsonKit.toEntity(json, SingleAuthUser.class);
        if(Objects.equals(authUser.getUuid(), decryptedAuthToken.getUuid()) &&
                Objects.equals(authUser.getLoginDeviceType(), decryptedAuthToken.getLoginDeviceType())) {
            return new GetAuthUserResult(true, authUser);
        }
        return new GetAuthUserResult(false, null);
    }


    @Override
    protected void setAuthUser(String key, AuthUser srcAuthUser, AuthToken decryptedAuthToken) {
        SingleAuthUser authUser = (SingleAuthUser) srcAuthUser;
        authUser.setUuid(decryptedAuthToken.getUuid());
        authUser.setLoginUserType(decryptedAuthToken.getLoginUserType());
        authUser.setLoginDeviceType(decryptedAuthToken.getLoginDeviceType());
        String json = JsonKit.toJSON(authUser);
        long expiration = this.getTimeunitUsedBySubclass().cast(decryptedAuthToken.getExpirationMill(), Timeunit.MILL);
        this.jedisClusterPlus.setex(key, expiration, json);
    }


    @Override
    protected void removeAuthUser(String key) {
        this.jedisClusterPlus.del(key);
    }

    @Override
    protected void updateAuthUser(String key, AuthUser authUser) {
        String json = (String) this.jedisClusterPlus.get(key);
        if(json == null) return;
        SingleAuthUser oldAuthUser = JsonKit.toEntity(json, SingleAuthUser.class);
        authUser.copyCustomData(oldAuthUser);
        authUser.copyAuthData(oldAuthUser);
        Object user = authUser.getUser();
        oldAuthUser.setUser(user);
        String newJson = JsonKit.toJSON(authUser);
        this.jedisClusterPlus.eval(UPDATE_AUTH_USER,
                0, key, newJson);
    }

    @Override
    protected void protected_loginOut(String key, String loginDeviceType, String uuid) {
        this.removeAuthUser(key);
    }
}
